//图片按比例缩放
function ResizeImage(ImgD, iwidth, iheight) {
    //参数(图片,允许的宽度,允许的高度)
    var image = new Image();
    image.src = ImgD.src;
    if (image.width > 0 && image.height > 0) {
        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.width = iwidth;
                ImgD.height = (image.height * iwidth) / image.width;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
            ImgD.alt = image.width + "×" + image.height;
        }
        else {
            if (image.height > iheight) {
                ImgD.height = iheight;
                ImgD.width = (image.width * iheight) / image.height;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
            ImgD.alt = image.width + "×" + image.height;
        }
    }
};


//用于ajax提交时,服务器返回异常时,客户端显示错误提示
function handleAjaxError() {
    // Initialize dialogs ...
    var dialogOptions = {
        autoOpen: false,
        draggable: false,
        modal: true,
        resizable: false,
        title: "Error",
        closeOnEscape: false,
        open: function () { $(".ui-dialog-titlebar-close").hide(); }, // Hide close button
        buttons: [{
            text: "Close",
            click: function () { $(this).dialog("close"); }
        }]
    };
    $("#InternalServerErrorDialog").dialog(dialogOptions);
    $("#NotFoundInfoDialog").dialog(dialogOptions);

    // Set up AJAX error handling ...
    $(document).ajaxError(function (event, jqXHR, ajaxSettings, thrownError) {
        if (jqXHR.status == 404) {
            $("#NotFoundInfoDialog").dialog("open");
        } else if (jqXHR.status == 500) {
            $("#InternalServerErrorDialog").dialog("open");
        } else {
            //alert("有个错误...");
        }
    });
};


//增加一个名为trim的函数作为String构造函数的原型对象的一个方法。
String.prototype.trim = function () {
    //   用正则表达式将前后空格
    //   用空字符串替代。
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

function waitBegin() {
    var $w = $('<div id="waitDialog"><span class="loading">正在处理...</span></div>');
    $w.dialog({
        width: 100,
        height: 100,
        modal: true
    });
};

function waitEnd() {
    var $w = $("div#waitDialog");
    $w.dialog('close');
};

function AsUrl(str) {
    return encodeURIComponent(str);
};


function GetElementCoordinate(e)
{
    var t = e.offsetTop;
    var l = e.offsetLeft;
    var w = e.offsetWidth;
    var h = e.offsetHeight;
    while (e = e.offsetParent)
    {
        t += e.offsetTop;
        l += e.offsetLeft;
    }
    return {
        top: t,
        left: l,
        width: w,
        height: h,
        bottom: t + h,
        right: l + w
    }
};

/*
var o = GetElCoordinate(document.getElementById("text-div"));
document.getElementById("test-div").style.left = parseInt(o.left)+"px";
document.getElementById("test-div").style.top = parseInt(o.bottom)+"px";
*/
